<?php

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;
use think\exception\HttpResponseException;

/**
 * 分类管理
 */
class GoodsCate extends BasicAdmin
{

    /**
     * 定义当前操作表名
     * @var string
     */
    public $goods_cate = 'jixie_goods_cate';

    /**
     * 分类列表
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $this->title = '分类';
        $db = Db::name($this->goods_cate)->where(['is_deleted' => '0']);
        return parent::_list($db->order('cate_id desc'), 'cate_id', false);
    }

    /**
     * 添加菜单
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        return $this->_form($this->goods_cate, 'form');
    }

    /**
     * 编辑菜单
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (!$this->request->isPost()) {
            $cate_id = $this->request->get('id');
            $cate = Db::name($this->goods_cate)->where(['cate_id' => $cate_id, 'is_deleted' => '0'])->find();
            empty($cate) && $this->error('需要编辑的分类不存在！');
            return $this->fetch('form', ['vo' => $cate, 'title' => '编辑分类']);
        }
        try {
            $cate_id = $this->request->post('id');
            $data = $this->request->post();
            $cate = Db::name($this->goods_cate)->where(['cate_id' => $cate_id, 'is_deleted' => '0'])->find();
            empty($cate) && $this->error('编辑分类失败，请稍候再试！');

            Db::transaction(function () use ($data, $cate_id, $cate) {
                $where = ['cate_id' => $cate_id, 'is_deleted' => '0'];
                Db::name($this->goods_cate)->where($where)->update($data);

            });

        } catch (HttpResponseException $exception) {
            return $exception->getResponse();
        } catch (\Exception $e) {
            $this->error('编辑分类失败，请稍候再试！' . $e->getMessage());
        }
        $this->success('编辑分类成功！', "");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->goods_cate)) {
            $this->success("分类删除成功！", '');
        }
        $this->error("分类删除失败，请稍候再试！");
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->goods_cate)) {
            $this->success("分类禁用成功！", '');
        }
        $this->error("分类禁用失败，请稍候再试！");
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->goods_cate)) {
            $this->success("分类启用成功！", '');
        }
        $this->error("分类启用失败，请稍候再试！");
    }

}
